package cn.gz.demo.ui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.AppBarLayout;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.adapter.RecyclerViewTablayoutListAdapter;
import cn.gz.demo.databinding.RecyclerviewTablayout2Binding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * @author Liuyz
 * @date 2020/10/10 17:50
 */
public class RecyclerviewTablayout2Activity extends BaseActivity {
    private RecyclerviewTablayout2Binding binding;
    private List<String> list = new ArrayList<>();
    private RecyclerViewTablayoutListAdapter adapter;
    private LinearLayoutManager manager;
    private TopSmoothScroller smoothScroller;
    private boolean tabCheck = false; // 是否tab切换角标
    private int tabIndex = -1; // tab角标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_tablayout2);

        list.clear();
        for (int i = 0; i < 15; i++) {
            list.add("第" + i + "条数据");
        }

        CommonNavigator navigator = new CommonNavigator(mContext);
        navigator.setAdjustMode(false);
        navigator.setScrollPivotX(0.75f);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return list == null ? 0 : list.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
//                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
//                simplePagerTitleView.setText(list.get(index));
//                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
//                simplePagerTitleView.setNormalColor(Color.parseColor("#8D8D99"));
//                simplePagerTitleView.setSelectedColor(Color.parseColor("#24272E"));
//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        scrollToTop();
//                        tabCheck = true;
//                        tabIndex = index;
//                        new Handler().postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                smoothScroller.setTargetPosition(index);  //要滑动到的位置
//                                manager.startSmoothScroll(smoothScroller);
//                            }
//                        }, 100);
//                    }
//                });
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.GRAY);
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setText(list.get(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
//                LinePagerIndicator indicator = new LinePagerIndicator(context);
//                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
//                indicator.setLineHeight(DisplayUtil.dip2px(context, 4));
//                indicator.setLineWidth(DisplayUtil.dip2px(context, 20));
//                indicator.setRoundRadius(DisplayUtil.dip2px(context, 3));
//                indicator.setStartInterpolator(new AccelerateInterpolator());
//                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
//                indicator.setColors(Color.parseColor("#fc5a44"));
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(Color.WHITE);
                return indicator;
            }
        });
        binding.indicator.setNavigator(navigator);

        smoothScroller = new TopSmoothScroller(context);

        manager = new LinearLayoutManager(context);
        binding.recyclerview.setLayoutManager(manager);
        adapter = new RecyclerViewTablayoutListAdapter(context, list);
        binding.recyclerview.setAdapter(adapter);

        binding.recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                binding.indicator.onPageScrollStateChanged(newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                int index = manager.findFirstVisibleItemPosition();
                if (tabCheck) {
                    binding.indicator.onPageSelected(tabIndex);
                    binding.indicator.onPageScrolled(tabIndex, 0, 1);
                    if (index == tabIndex) {
                        tabCheck = false;
                    }
                } else {
                    binding.indicator.onPageSelected(index);
                    binding.indicator.onPageScrolled(index, 0, 1);
                }
            }
        });
    }

    class TopSmoothScroller extends LinearSmoothScroller {

        public TopSmoothScroller(Context context) {
            super(context);
        }

        @Override
        protected int getHorizontalSnapPreference() {
            return SNAP_TO_START;
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }

    /**
     * 一键吸顶
     */
    public void scrollToTop() {
        //拿到 appbar 的 behavior,让 appbar 滚动
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) binding.appBarLayout.getLayoutParams()).getBehavior();
        if (behavior instanceof AppBarLayout.Behavior) {
            AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
            //拿到下方tabs的y坐标，即为我要的偏移量
            float y = binding.indicatorLayout.getY();
            //注意传递负值
            appBarLayoutBehavior.setTopAndBottomOffset((int) -y);
        }
    }
}
